<?php
require_once ("BaseDAO.php");

// UserRoleMap Value Object
class UserRoleMap extends BaseVO {
	var $id;
	var $user_id;
	var $role;

	// create a new VO
	function UserRoleMap(
		$id= 0,
		$user_id= 0,
		$role= ""
	) {
		$this->id = $id;
		$this->user_id = $user_id;
		$this->role = $role;
	}

	// compare to another VO
	function equals($vo) {
		return $this->id == $vo->id &&
			$this->user_id == $vo->user_id &&
			$this->role == $vo->role;
	}
	// copies another VO
	function copy($vo) {
		$this->id = $vo->id;
		$this->user_id = $vo->user_id;
		$this->role = $vo->role;
	}

	// output as a string
	function toString() {
		return $this->id . "," . $this->user_id . "," . $this->role;
	}

	// output as XML node
	function toXML() {
		return "<row>\n" .
		"<id>$this->id</id>\n" .
		"<user_id>$this->user_id</user_id>\n" .
		"<role>$this->role</role>\n" .
		"</row>\n";
	}

	// read from an html form
	function readForm($prefix="") {
		$this->id = $this->formHelper($prefix . "id", 0);
		$this->user_id = $this->formHelper($prefix . "user_id", 'null');
		$this->role = $this->formHelper($prefix . "role", "");
	}

	// read from the query string
	function readQuery($prefix="") {
		$this->id = $this->queryHelper($prefix . "id", 0);
		$this->user_id = $this->queryHelper($prefix . "user_id", 'null');
		$this->role = $this->queryHelper($prefix . "role", "");
	}

	// extra functions here
	function getPK() {
		return $this->id;
	}
}

// UserRoleMap Data Access Object
class UserRoleMapDAO extends BaseDAO {
	var $SQL_SELECT = "SELECT * FROM `user_role_map` ";
	var $SQL_COUNT = "SELECT count(*) AS cnt FROM `user_role_map` ";
	// insert with no value for an auto_increment field.
	var $SQL_INSERT = "INSERT INTO `user_role_map` (user_id,role) VALUES (%A,'%B')";
	var $SQL_UPDATE = "UPDATE `user_role_map` SET ";
	var $SQL_DELETE = "DELETE FROM `user_role_map` WHERE id=%A";

	// default constructor
	function UserRoleMapDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
		// calls the parent constructor which
		// makes the database connection.
		parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
	}

	// returns a single UserRoleMap obeject
	function get($id) {    
	$this->sql = $this->SQL_SELECT . "WHERE (id='" . $this->safeEscapeStr($id) . "')";
		$this->exec($this->sql);
		if($this->numRows() > 0 ) {
			$row = $this->getObject();
			$vo = new UserRoleMap(
				$row->id, 
				$row->user_id, 
				$row->role
			);
			return $vo;
		} else {
			return null;    
		}
	}

	// returns an array of UserRoleMap objects
	// given a complete SQL query 
	// The SQL Query needs to be safely escaped by the caller!
	function findBySQL($sql) {
		$voList = array();
		$this->sql = $sql;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new UserRoleMap(
				$row->id, 
				$row->user_id, 
				$row->role
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of UserRoleMap objects
	// value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
	// value for $order_by is a comma separated list of columns "company, name"
	// the where and order clauses should be safely escaped by the caller!
	function findWhere($where = "", $orderby = "") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		if(strlen($where) > 0 ) {
			$where = "WHERE (" . $where . ") ";
		}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new UserRoleMap(
				$row->id, 
				$row->user_id, 
				$row->role
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of UserRoleMap objects based on the example
	function findExample($example) {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		$where = "";

		$tmp = "";
		if($example->id != 0) $tmp .= "id = " . $this->safeEscapeStr($example->id) . " and ";
		if($example->user_id != 0) $tmp .= "user_id = " . $this->safeEscapeStr($example->user_id) . " and ";
		if(strlen($example->role) > 0) $tmp .= "role like '%" . $this->safeEscapeStr($example->role) . "%' and ";
		if ($tmp != "") {$where = "WHERE (" . substr($tmp, 0, strlen($tmp)-4) . ") ";}
		$this->sql .= $where;

		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new UserRoleMap(
				$row->id, 
				$row->user_id, 
				$row->role
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// insert a record from a vo
	function insert(&$vo) {
		$this->sql = $this->SQL_INSERT;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->user_id), $this->sql);
		$this->sql = str_replace("%B", $this->safeEscapeStr($vo->role), $this->sql);

		$this->exec($this->sql);
		$vo->id = $this->lastOID();
		return $this->affectedRows();
	}

	// update a record from a vo
	function update($vo) {
		$this->sql = $this->SQL_UPDATE;
		$this->sql .= "user_id = " . $this->safeEscapeStr($vo->user_id) . ", ";
		$this->sql .= "role = '" . $this->safeEscapeStr($vo->role) . "' ";

		$this->sql .= "WHERE id=" . $this->safeEscapeStr($vo->id);

		$this->exec($this->sql);
		return $this->affectedRows();
	}

	//save an object to the databse (update or insert as needed)
	function save(&$vo) {
		if ($vo->id <= 0)
			return $this->insert($vo);
		else
			return $this->update($vo);
	}

	// delete a record from a vo
	function delete($vo) {
		$this->sql = $this->SQL_DELETE;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->id), $this->sql);
		$this->exec($this->sql);
		return $this->affectedRows();
	}

}

